package edu.fzu.costlog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fzu.costlog.domain.CostCategory;
import edu.fzu.costlog.domain.CostLog;
import edu.fzu.costlog.service.CostCategoryService;
import edu.fzu.costlog.service.CostCategoryServiceImpl;
import edu.fzu.costlog.service.CostLogService;
import edu.fzu.costlog.service.CostLogServiceImpl;

public class CostCategoryMgrServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act = request.getParameter("act");

		if (act.equals("save")) {

			CostCategory category = new CostCategory();

			category.setCategoryName(request.getParameter("categoryname"));
			System.out.println(request.getParameter("categoryname"));
			category.setCreateTime(new Date());

			CostCategoryService categoryService = new CostCategoryServiceImpl();
			categoryService.saveCostCategory(category);

			response.sendRedirect("costCategoryMgr?act=loadall");
		} else if (act.equals("loadall")) {

			CostCategoryService categoryService = new CostCategoryServiceImpl();
			List<CostCategory> categoryList = categoryService.loadall();

			request.setAttribute("categorys", categoryList);
			request.getRequestDispatcher("show_costcategorys.jsp").forward(
					request, response);
		} else if (act.equals("del")) {
			int categoryId = Integer.parseInt(request
					.getParameter("category_id"));

			CostCategoryService categoryService = new CostCategoryServiceImpl();
			categoryService.removeCostCategory(categoryId);

			response.sendRedirect("costCategoryMgr?act=loadall");
		} else if (act.equals("getCategoryById")) {

			int categoryId = Integer.parseInt(request
					.getParameter("categoryid"));

			CostCategoryService categoryService = new CostCategoryServiceImpl();
			request.setAttribute("category", categoryService
					.getCostCategoryById(categoryId));

			request.getRequestDispatcher("update_costcategory.jsp").forward(
					request, response);
		} else if (act.equals("update")) {

			CostCategory category = new CostCategory();

			category.setCategoryId(Integer.parseInt(request
					.getParameter("category_id")));
			category.setCategoryName(request.getParameter("categoryname"));
			category.setCreateTime(new Date());

			CostCategoryService CategoryService = new CostCategoryServiceImpl();
			CategoryService.updateCostCategory(category);

			response.sendRedirect("costCategoryMgr?act=loadall");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
